<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
    <template v-slot:object="{row}">
      <p v-if="!row.value.model_name">默认</p>
      <p v-else>
        <span>【{{model_map[row.value.model_name]}}】</span>
        <span v-if="row.value.object_title">{{row.value.object_title}}</span>
        <span v-else>全部</span>
      </p>
    </template>
    <template v-slot:kickback="{row}">
      <input type="hidden" :name="row.name" :value="encode_json(row.value)">
      <div class="flex">
        <label v-for="v in level_list" style="width: 150px;">
          <span class="text-ellipsis">{{v.name}}</span>
          <el-input v-model="row.value[v.id]" type="number" style="width: 120px;">
            <template #append >{{!~[0,2].indexOf(row.config.type)?'元':'%'}}</template>
          </el-input>
        </label>
      </div>
    </template>
  </forms>
</template>

<script>
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {_config, decode_config} from "@/js/s94_tool.js";
import s94Mjs from "s94-js";

export default {
  props: ['__iframe'],
  data(){
    let app = this, info_param = '', btns = undefined;
    let object = {
      object_id: this.$get('object_id'),
      model_name: this.$get('model_name'),
      object_title: this.$get('object_title'),
    }
    if (typeof(object.object_id) !== 'undefined'){
      info_param = `model_name=${object.model_name}&object_id=${object.object_id}`;
    }
    var status_config = _config('status_alias_map.kickback_ratio.status');
    status_config = status_config.slice(0,2);
    let model_list = [
      {label:'类目', name:'category'}
    ].concat(Object.values(_config('model_map')));
    let model_name_config = decode_config(model_list, {content_key:'label', value_key:'name'})
    let forms_config = {
      object_title: {label:'项目详情',name:'object_title',value:""},
      model_name: {label:'项目类型',name:'model_name',type:'select_one',config:model_name_config,tips:'',value:''},
      object_id: {label:'项目ID',name:'object_id',type:'text_one',tips:'如果设置为0，表示该类型的所有项目',config:'',value:0},
      status: {label:'状态',name:'status',type:'radio_switch',config:status_config,tips:'',value:1},
    };
    if (info_param){
      forms_config.model_name.remove = forms_config.object_id.remove = true;
      if (!object.model_name) forms_config.status.remove = true;
    }else {
      forms_config.object_title.remove = true;
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      info_param: info_param,
      btns: btns,
      config: forms_config,
      object: object,
      level_list: [],
      model_map: {},
    }
  },
  methods: {
    onsubmit(formData){
      let url = this.info_param ? `/admin/kickback_ratio/edit?`+this.info_param : '/admin/kickback_ratio/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('kickback_ratio/index', '$refs.list_url.list');
          iframe.hide('kickback_ratio/edit');
          if (!this.info_param) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    encode_json(arr){
      let res = {};
      s94Mjs.each(arr, function (v,k){
        if (v != '') res[k] = Number(v);
      })
      return JSON.stringify(res);
    },
    change_kickback(row,lid,v){
      if (v=='') {
        delete row.value[lid];
      }else {
        row.value[lid] = Number(v);
      }
    },
  },
  watch:{
    'config.model_name.value'(v,oldV){
      let app = this;
      app.config.status.remove = !v;
      if (v=="user_money_recharge"){
        app.config.object_id.remove = true;
        app.config.object_id.value = '0';
        return;
      }
      app.config.object_id.remove = false;
      if (v=='category'){
        app.config.object_id.config = 'category/index';
      }else {
        app.config.object_id.config = 'model_base/index?model_name='+v;
      }
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    let init_data = undefined;
    if (this.info_param) {
      ajax({url:'/admin/kickback_ratio/info?'+this.info_param, cache:true}, (res)=>{
        init_data = {
          status: res.data[0]['status'],
          object_title: res.data[0]['object_title'],
        }
        s94Mjs.each(res.data,function(v){
          let name = 'kickback['+v.kickback_type_id+']';
          init_data[name] = init_data[name] || {};
          init_data[name][v.user_level_id] = v.ratio;
        })
        app.$refs['forms'].init(init_data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('kickback_ratio/edit');
        }else {
          this.$router.push('index');
        }
      })
    }

    ajax({url:'/admin/kickback_ratio/config_map',cache:true}, function (res){
      let {user_level,kickback_type,model_list} = res.data;
      app.level_list = user_level;

      Object.keys(app.config).forEach(function (k){
        if (/kickback_\d+/.test(k)) delete app.config[k];
      });
      Object.values(kickback_type).forEach(v=>{
        if (!~[0,2].indexOf(v.type)) { // 非【订单分佣】【邀请下单分佣】的佣金类型，在 添加 或者 个性化比例的编辑 时不显示
          if (!app.info_param || app.object.model_name) return;
        }
        app.config['kickback_'+v.id] = {label:v.title, name:'kickback['+v.id+']', type:'kickback',tips:'',value:{}, config:v};
      })
      if (init_data) app.$refs['forms'].init(init_data);

    });

  }
}
</script>
